﻿using System;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using Logistics.DataManage;
using System.Collections.Generic;
using Logistics.Utils;
using Logistics.DataManage.DAL;
using Logistics.DataManage.Local;
using MapgisEbp.QuickDataBase;

namespace Logistics.Control
{
    /// <summary>
    /// Interaction logic for RecordGridPanel.xaml
    /// </summary>
    public partial class RecordGridPanel : UserControl
    {
        public int PageSize { get { return this.MyPager.PageSize; } set { this.MyPager.PageSize = value; } }
        private QueryConditions Conditions { get; set; }

        public RecordGridPanel()
        {
            InitializeComponent();

            PageSize = 20;
            Conditions = new QueryConditions();
        }

        public String SelectedItem
        {
            get 
            {
                Object obj = this.DataGridList.SelectedItem;
                OWaybill Bill = obj as OWaybill;
                if (Bill != null)
                    return Bill.WaybillID;
                return null; 
            }
        }

        public void ReloadData()
        {
            this.Conditions = new QueryConditions();
            QueryAndFillData(this.Conditions, 0);
            this.MyPager.PageIndex = 1; //复位
        }

        private void ButtonQuery_Click(object sender, RoutedEventArgs e)
        {
            String userId = this.txtReceiver.Text;
            this.Conditions.billNo = this.txtBillNo.Text;
            this.Conditions.receiver = this.txtReceiver.Text;
            this.Conditions.sender = this.txtSender.Text;
            this.Conditions.startDt = this.txtDateStart.Value;
            this.Conditions.endDt = this.txtDateEnd.Value;

            QueryAndFillData(this.Conditions, 0);
        }
        public void QueryAndFillData(QueryConditions conditions,int pageIndex)
        {
            int total = GetUserBillsCount(conditions);
            this.MyPager.Total = total;
            DataSet ds = QueryUserBills(conditions, PageSize, pageIndex);
            IList<OWaybill> list = DataConvert.ConvertToEntity<OWaybill>(ds);
            this.DataGridList.ItemsSource = list;

            this.MyPager.ItemsSource = list;
        }
        private void MyPager_PageChanged(object sender, PageChangedEventArgs args)
        {
            this.PageSize = args.PageSize;
            //args.PageIndex 是从1开始计数的,所以这里减1
            QueryAndFillData(this.Conditions, args.PageIndex-1);
        }

        #region 列表数据查询
        private DataSet QueryUserBills(QueryConditions conditions,
            int pageSize, int pageIndex)
        {
            int offset = pageSize * pageIndex;
            String pager = "limit " + offset + "," + pageSize;
            IDataParameter[] paras;
            String condition = GenerateWhere(conditions, out paras);
            String sql = "select * from WaybillInfo where " + condition + pager;
            DataSet ds = LocalDb.Handler.ExecuteQuery(sql, paras);
            return ds;
        }
        private int GetUserBillsCount(QueryConditions conditions)
        {
            IDataParameter[] paras;
            String condition = GenerateWhere(conditions, out paras);
            String sql = "select count(WaybillID) from WaybillInfo where " + condition;
            Object count = LocalDb.Handler.ExecuteScalar(sql, paras);
            return Convert.ToInt32(count);
        }

        private String GenerateWhere(QueryConditions conditions,
            out IDataParameter[] paras)
        {
            List<Object> lparas = new List<object>();
            String sql = " Isused='1' ";//Isused是否已上传
            if (!String.IsNullOrEmpty(conditions.createUserName))
            {
                sql += " and CreateUser=?";
                lparas.Add(conditions.createUserName);
            }
            if (!String.IsNullOrEmpty(conditions.billNo))
            {
                sql += " and WaybillID like ?";
                lparas.Add("%" + conditions.billNo + "%");
            }
            if (!String.IsNullOrEmpty(conditions.sender))
            {
                sql += " and Consignor like ?";
                lparas.Add("%" + conditions.sender + "%");
            }
            if (!String.IsNullOrEmpty(conditions.receiver))
            {
                sql += " and Consigneeor like ?";
                lparas.Add("%" + conditions.receiver + "%");
            }
            if (conditions.startDt != null)
            {
                sql += " and CreatedDate > ?";
                lparas.Add(conditions.startDt.Value);
            }
            if (conditions.endDt != null)
            {
                sql += " and CreatedDate < ?";
                lparas.Add(conditions.endDt.Value);
            }
            ParameterizedSQL PSQL = LocalDb.Handler.DbAdapter.AdaptSQLAnonymousParams(sql, lparas.ToArray());
            paras = PSQL.Parameters;
            return PSQL.SQL;
        }
        #endregion
    }
    #region
    public class QueryConditions
    {
        public String createUserName;
        public String billNo;
        public String sender;
        public String receiver;
        public DateTime? startDt;
        public DateTime? endDt;
    }
    #endregion
}
